package com.mlizhi.modules.spec.content;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mlizhi.base.imageloader.core.DisplayImageOptions;
import com.mlizhi.base.imageloader.core.ImageLoader;
import com.mlizhi.kingdom.R;
import com.mlizhi.utils.Constants;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015-03-23.
 */
public class ContentAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局
    private List<HashMap<String,String>> contentList;
    DisplayImageOptions displayImageOptions =null;
    private int time;//2表示今天，1表示全部
    
    
    public ContentAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_content_preview_holder) // resource or drawable
                .showImageForEmptyUri(R.drawable.ic_content_preview_holder) // resource or drawable
                .showImageOnFail(R.drawable.ic_content_preview_holder) // resource or drawable
                .cacheInMemory(false) // default
                .cacheOnDisk(true) // default
                .build();
    }

    

    public List<HashMap<String, String>> getContentList() {
		return contentList;
	}



	public void setContentList(List<HashMap<String, String>> contentList) {
		this.contentList = contentList;
	}



	@Override
    public int getCount() {
        return contentList.size();
    }

    @Override
    public Object getItem(int position) {
        return contentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**书中详细解释该方法*/
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //观察convertView随ListView滚动情况
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.activity_spec_content_item,null);
            holder = new ViewHolder();
            /**得到各个控件的对象*/
            holder.contentTitleImage = (ImageView) convertView.findViewById(R.id.content_title_image_item);
            holder.contentTitle = (TextView) convertView.findViewById(R.id.content_title_item);
            holder.customerPraise = (TextView) convertView.findViewById(R.id.content_customer_operator_praise_value);
            holder.customerBrowse = (TextView) convertView.findViewById(R.id.content_customer_operator_browse_value);
            
            convertView.setTag(holder);//绑定ViewHolder对象
        }  else{
            holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象
        }

        HashMap<String,String> contentHashMap = contentList.get(position);
        ImageLoader.getInstance().displayImage(contentHashMap.get(Constants.CONTENT_ITEM_IMAGE),holder.contentTitleImage,displayImageOptions);
        holder.contentTitle.setText(contentHashMap.get(Constants.CONTENT_ITEM_TITLE));
        holder.customerPraise.setText(contentHashMap.get(Constants.CONTENT_ITEM_PRAISE_NUM));
        holder.customerBrowse.setText(contentHashMap.get(Constants.CONTENT_ITEM_VIEW_NUM));
        return convertView;
    }
    /**存放控件*/
    static class ViewHolder{

        public ImageView contentTitleImage;
        public TextView contentTitle;
        public TextView customerPraise;
        public TextView customerBrowse;

    }
    
    
}


